PROGRAM_NAME='Melton Main'
(***********************************************************)
(***********************************************************)
(*  FILE_LAST_MODIFIED_ON: 04/05/2006  AT: 09:00:25        *)
(***********************************************************)
(* System Type : NetLinx                                   *)
(***********************************************************)
(* REV HISTORY:                                            *)
(***********************************************************)
(*
    $History: $
*)
#DEFINE DEBUG
(***********************************************************)
(*          DEVICE NUMBER DEFINITIONS GO BELOW             *)
(***********************************************************)

// Jim was here.  You lucky bastard.
DEFINE_DEVICE
dvTmTimeSync      = 0:11:0
vdvTmEvents       = 33201:1:0

dvIPReporter      = 0:12:0

dvDebugLog        = 0:13:0
vdvDebugLog       = 33100:1:1

dvWeather         = 0:14:0

dvDTV_IP          = 0:16:0

dvStat1           = 0:15:0
vdvStat1          = 33101:1:1

dvAudioSwitcher   = 6001:1:1
vdvAudioSwitcher  = 33102:1:1

dvVHMControl	= 0:18:0
vdvVHMControl	= 33111:1:1

dvVHMControlCallHome	= 0:19:0
vdvVHMControlCallHome	= 33112:1:1

dvVHMAV		= 0:20:0
vdvVHMAV	= 33113:1:1

// Serial
dvFamilyAVR  = 375:1:1
vdvFamilyAVR = 33103:1:1

dvFamilyDTV  = 0:21:0     // DirecTv HR21 IR Port #2
vdvFamilyDTV = 33105:1:0    // Virtual for serial control

// IR
dvFamilyTV   = 373:1:0     // Mitsu IR Port #1
vdvFamilyTV  = 33104:1:1

dvFamilyDVD  = 374:2:0    // Hitacthi HDDVD IR Port #4
vdvFamilyDVD = 33106:11:0    // Hitacthi HDDVD IR Port #4

dvFamilyCD   = 374:1:0    // Marantz CD67SE IR Port #7
vdvFamilyCD  = 33107:1:0    // Marantz CD67SE IR Port #7

// Relays
dvRelay      = 377:1:1

vdvHDSwitcher = 33108:1:1   // Relay controlled Extron component switcher
//dvKitchenPC  = 33109:1:0    // 

vdvDummySwitcher = 33120:1:1
vdvDummyPanel    = 33121:1:1
vdvDummyAVR      = 33122:1:1
vdvDummyTV       = 33123:1:1

vdvGlobalComms   = 33114:1:1
vdvGlobalShareComms = 33115:1:1


// Audio VIdeo Zones
vdvInterRoomComms     = 33000:1:1

vdvFamilyAV 	      = 33301:1:1
vdvKitchenA           = 33302:1:1
vdvPatioA             = 33303:1:1
vdvGarageA            = 33304:1:1
vdvMBedAV             = 33305:1:2
vdvMBathA             = 33306:1:1

dvKitchenKP           = 6011:1:1
dvMBathKP             = 6012:1:1
dvGarageKP            = 6013:1:1

dvIpad                = 11001:1:1
dvFamilyTP            = 11002:1:1
vdvKitchenKP          = 33402:1:1
vdvGarageKP           = 33403:1:1
dvMBedTP              = 11003:1:1
vdvMBathKP            = 33405:1:1

dvWeatherIpad         = 11001:4:1
dvWeatherFamilyTP     = 11002:4:1
dvWeatherMBedTP       = 11003:4:1

vdvMBedDTV = 33001:1:2
vdvMBedTV = 33002:1:2
vdvMBedAVR = 33004:1:2

dvMBedDVD = 5001:10:2
vdvMBedDVD = 33110:1:1

(***********************************************************)
(*               CONSTANT DEFINITIONS GO BELOW             *)
(***********************************************************)
DEFINE_CONSTANT
#Include 'Melton Common Include.axi'

(***********************************************************)
(*              DATA TYPE DEFINITIONS GO BELOW             *)
(***********************************************************)
DEFINE_TYPE

(***********************************************************)
(*               VARIABLE DEFINITIONS GO BELOW             *)
(***********************************************************)
DEFINE_VARIABLE

Volatile _sDMS sDMS[cnMaxUIs]
Volatile _sAudioSwitcher sAudioSwitcher[1]

Volatile Integer nAudioSwitcherLevels[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}

Volatile Char strClientKeyAV[] = 'omXvMB' // unique per subsystem
Volatile Char strClientKeyControl[] = 'Uyiwhh' // unique per subsystem
Volatile Char strClientName[] = 'Melton'
Volatile Char strDestinationEmail[] = 'meltonrussell@gmail.com'
Volatile Char strJunk[10]

Volatile Char strSatFavsFileName[] = 'SAT_FAVS.xml'
Volatile Char strSatMusicFavsFileName[] = 'SAT_MUSIC_FAVS'

Volatile Integer nWeatherButtons[] = {550,551,552,553} // F, C, force new data, zip code toggle

Volatile Char strWeatherZipCodes[1][5]
Volatile Integer nWeatherZipCodes = 1
Volatile Integer nWeatherTextChans[] =
{
    550,  // 1- Temp
    551,  // 2- Humidity
    595,  // 3- Mode
    584,  // 4- Wind Chill
    554,  // 5- Day 1 Forecast High
    555,  // 6- Day 1 Forecast Low
    596,  // 7- Day 1 Forecast Mode
    590,  // 8- Day 2 Forecast High
    591,  // 9- Day 2 Forecast Low
    597,  // 10- Day 2 Forecast Mode
    552,  // 11- Condition
    999,  // 12- Image Filename not used
    13,   // 13- Visibility
    553,  // 14- Wind
    583,  // 15- Pressure
    999,  // 16- Sunrise
    999,  // 17- Sunset
    557,  // 18- Current Location
    558,  // 19- Day 1 Forecast Day
    999,  // 20- Day 1 Forecast Condition
    560,  // 21- Day 2 Forecast Day
    999,  // 22- Day 2 Forecast Condition
    562   // 23- Last Updated
}         

Volatile Integer nWeatherFeedbackChans[] = {1}

Volatile Char ciViewerFilename[]='MeltonIpad.gui'
Volatile Char ciViewerPswd[]=''
Volatile Integer niViewerDebug = 1

Volatile Dev daAllWeatherUIs[] =
{
    dvWeatherIpad,
    dvWeatherFamilyTP,
    dvWeatherMBedTP
}

Volatile Dev daAllDMS[] =
{
    vdvKitchenKP,
    vdvGarageKP,
    vdvMBathKP
}

Volatile Integer nAllTPTypes[] =
{
    cnPanelTypeTP,
    cnPanelTypeTP,
    cnPanelTypeKP,
    cnPanelTypeKP,
    cnPanelTypeTP,
    cnPanelTypeKP
}

Volatile Dev daAllRooms[] =
{
    vdvFamilyAV,
    vdvKitchenA,
    vdvPatioA,
    vdvGarageA,
    vdvMBedAV,
    vdvMBathA
}

Volatile Dev daFamilyPanels[cnMaxUIs]
Volatile Dev daKitchenPanels[cnMaxUIs]
Volatile Dev daPatioPanels[cnMaxUIs]
Volatile Dev daGaragePanels[cnMaxUIs]
//Volatile Dev daMBedPanels[cnMaxUIs]
Volatile Dev daMBathPanels[cnMaxUIs]

Volatile Dev daFamilyDtvPanels[cnMaxUIs]
//Volatile Dev daMBedDtvPanels[cnMaxUIs]
Volatile Dev daFamilyDvdPanels[cnMaxUIs]
//Volatile Dev daMBedDvdPanels[cnMaxUIs]
Volatile Dev daFamilyCdPanels[cnMaxUIs]
Volatile Dev daFamilySatMusicPanels[cnMaxUIs]

Volatile Char strFamilyDTVIpAddress[] = '192.168.0.122'

Volatile Dev daAllTimerTps[] =
{
    dvIpad,
    dvFamilyTP,
    dvMBedTP
//    vdvKitchenKP,
//    vdvGarageKP,
//    vdvMBathKP
}

Volatile Integer nTimer1RelayChans[] = {1,3} // Upper Light
// Hour +, Hour -, Minute Plus, Minute Minus, Set On/Off Time, Enable/Disable
Volatile Integer nTimer1Buttons[] = {2001,2002,2003,2004,2005,2006}
Volatile Integer nTimer1VarText[] = {2001,2002}
Volatile Char strTimer1FileName[] = 'Timer1.xml'

Volatile Integer nTimer2RelayChans[] = {2,4} // Upper Fan
Volatile Integer nTimer2Buttons[] = {2011,2012,2013,2014,2015,2016}
Volatile Integer nTimer2VarText[] = {2011,2012}
Volatile Char strTimer2FileName[] = 'Timer2.xml'

Volatile Integer nTimer3RelayChans[] = {6,8} // Lower Light
Volatile Integer nTimer3Buttons[] = {2021,2022,2023,2024,2025,2026}
Volatile Integer nTimer3VarText[] = {2021,2022}
Volatile Char strTimer3FileName[] = 'Timer3.xml'

Volatile Integer nTimer4RelayChans[] = {7,9} // Lower Fan
Volatile Integer nTimer4Buttons[] = {2031,2032,2033,2034,2035,2036}
Volatile Integer nTimer4VarText[] = {2031,2032}
Volatile Char strTimer4FileName[] = 'Timer4.xml'
(***********************************************************)
(*               LATCHING DEFINITIONS GO BELOW             *)
(***********************************************************)
DEFINE_LATCHING

(***********************************************************)
(*       MUTUALLY EXCLUSIVE DEFINITIONS GO BELOW           *)
(***********************************************************)
DEFINE_MUTUALLY_EXCLUSIVE
([dvIpad,nGlobalGroupSelectButtons[1]]..[dvIpad,nGlobalGroupSelectButtons[10]])
([dvFamilyTP,nGlobalGroupSelectButtons[1]]..[dvFamilyTP,nGlobalGroupSelectButtons[10]])
([dvMBedTP,nGlobalGroupSelectButtons[1]]..[dvMBedTP,nGlobalGroupSelectButtons[10]])

(***********************************************************)
(*        SUBROUTINE/FUNCTION DEFINITIONS GO BELOW         *)
(***********************************************************)
(* EXAMPLE: DEFINE_FUNCTION <RETURN_TYPE> <NAME> (<PARAMETERS>) *)
(* EXAMPLE: DEFINE_CALL '<NAME>' (<PARAMETERS>) *)
#INCLUDE 'Melton Main - Panels.axi'

(***********************************************************)
(*                STARTUP CODE GOES BELOW                  *)
(***********************************************************)
DEFINE_START

// DMS Setup
sDMS[cnKitchenKPID].strFileName = 'Melton - DMS.kpd'
sDMS[cnGarageKPID].strFileName  = 'Melton - DMS.kpd'
sDMS[cnMBathKPID].strFileName   = 'Melton - DMS.kpd'

strWeatherZipCodes[1] = '81647'

Wait 200
{
    Set_Length_Array(daFamilyDtvPanels,0)
    Set_Length_Array(daFamilyDvdPanels,0)
    Set_Length_Array(daFamilyCdPanels,0)
    Set_Length_Array(daFamilySatMusicPanels,0)

    Set_Length_Array(daFamilyPanels,2)
    Set_Length_Array(daKitchenPanels,1)
    Set_Length_Array(daPatioPanels,0)
    Set_Length_Array(daGaragePanels,1)
    Set_Length_Array(daMBathPanels,1)

    sUI[cnIpadID].nZone = cnFamilyID
    sUI[cnFamilyTPID].nZone = cnFamilyID
    sUI[cnKitchenKPID].nZone = cnKitchenID
    sUI[cnGarageKPID].nZone = cnGarageID
    sUI[cnMBedTPID].nZone = cnMBedID
    sUI[cnMBathKPID].nZone = cnMBathID

    sUI[cnIpadID].nZoneListLen = 8
    sUI[cnFamilyTPID].nZoneListLen = 4
    sUI[cnKitchenKPID].nZoneListLen = 0
    sUI[cnGarageKPID].nZoneListLen = 0
    sUI[cnMBedTPID].nZoneListLen = 4
    sUI[cnMBathKPID].nZoneListLen = 0

    daFamilyPanels[1] = dvIpad
    daFamilyPanels[2] = dvFamilyTP
    daKitchenPanels[1] = vdvKitchenKP
    //daPatioPanels[]
    daGaragePanels[1] = vdvGarageKP
    daMBathPanels[1] = vdvMBathKP

    Call 'SETUP_SOURCES_AUDIO' (vdvKitchenA)
    Call 'SETUP_SOURCES_AUDIO' (vdvPatioA)
    Call 'SETUP_SOURCES_AUDIO' (vdvGarageA)
    Call 'SETUP_SOURCES_AUDIO' (vdvMBathA)
	

    // Format: SOURCE1=nSourceValue, nGlobalSource, nAudioInput, nVideoInput, nComponentInput, nAVRInput, nTVInput
    // nSourceValue: Unique source identifier
    // nGlobalSource: 0 = false, 1 = true
    // nAudioInput: Input used on the audio switcher this zone is connected to
    // nVideoInput: Input used on the video switcher this zone is connected to
    // nComponentInput: Input used on the HD switcher this zone is connected to
    // nAVRInput: Channel used to turn on AVR and select the correct input in its module
    // nTVInput: Channel used to turn on TV and select the correct input in its module
    // strName: Friendly name

    Send_Command vdvFamilyAV,"'SOURCE1=',Itoa(sSourceInfo[cnFamilyDtvID].nSourceValue),',',Itoa(sSourceInfo[cnFamilyDtvID].nAudioInput),',',Itoa(sSourceInfo[cnFamilyDtvID].nVideoInput),',',Itoa(sSourceInfo[cnFamilyDtvID].nHDInput),',13,5,',sSourceInfo[cnFamilyDtvID].strName"
    Send_Command vdvFamilyAV,"'SOURCE2=',Itoa(sSourceInfo[cnFamilyDvdID].nSourceValue),',',Itoa(sSourceInfo[cnFamilyDvdID].nAudioInput),',',Itoa(sSourceInfo[cnFamilyDvdID].nVideoInput),',',Itoa(sSourceInfo[cnFamilyDvdID].nHDInput),',18,5,',sSourceInfo[cnFamilyDvdID].strName"
    Send_Command vdvFamilyAV,"'SOURCE3=',Itoa(sSourceInfo[cnFamilyPs3ID].nSourceValue),',',Itoa(sSourceInfo[cnFamilyPs3ID].nAudioInput),',',Itoa(sSourceInfo[cnFamilyPs3ID].nVideoInput),',',Itoa(sSourceInfo[cnFamilyPs3ID].nHDInput),',10,7,',sSourceInfo[cnFamilyPs3ID].strName"
    Send_Command vdvFamilyAV,"'SOURCE4=',Itoa(sSourceInfo[cnKitchenPCID].nSourceValue),',',Itoa(sSourceInfo[cnKitchenPCID].nAudioInput),',',Itoa(sSourceInfo[cnKitchenPCID].nVideoInput),',',Itoa(sSourceInfo[cnKitchenPCID].nHDInput),',19,6,',sSourceInfo[cnKitchenPCID].strName"
    Send_Command vdvFamilyAV,"'SOURCE5=',Itoa(sSourceInfo[cnFamilyIpodID].nSourceValue),',',Itoa(sSourceInfo[cnFamilyIpodID].nAudioInput),',',Itoa(sSourceInfo[cnFamilyIpodID].nVideoInput),',',Itoa(sSourceInfo[cnFamilyIpodID].nHDInput),',20,2,',sSourceInfo[cnFamilyIpodID].strName"
    Send_Command vdvFamilyAV,"'SOURCE6=',Itoa(sSourceInfo[cnFamilyCdID].nSourceValue),',',Itoa(sSourceInfo[cnFamilyCdID].nAudioInput),',',Itoa(sSourceInfo[cnFamilyCdID].nVideoInput),',',Itoa(sSourceInfo[cnFamilyCdID].nHDInput),',20,2,',sSourceInfo[cnFamilyCdID].strName"
    Send_Command vdvFamilyAV,"'SOURCE7=',Itoa(sSourceInfo[cnSatMusicID].nSourceValue),',',Itoa(sSourceInfo[cnSatMusicID].nAudioInput),',',Itoa(sSourceInfo[cnSatMusicID].nVideoInput),',',Itoa(sSourceInfo[cnSatMusicID].nHDInput),',13,2,',sSourceInfo[cnSatMusicID].strName"
    
    // Format: AVR=Off,VolUp,VolDown,MuteToggle,VolLevel,DirectVolInLevel,StartVol
    Send_Command vdvFamilyAV,"'AVR=28,1,2,3,1'"

    //////////////////
    // Global Audio //
    //////////////////
    // All
    Send_Command vdvGlobalComms,"'GROUP1=',Itoa(sRoomInfo[cnFamilyID].nID),':',sRoomInfo[cnFamilyID].strName,','
					  ,Itoa(sRoomInfo[cnKitchenID].nID),':',sRoomInfo[cnKitchenID].strName,','
					  ,Itoa(sRoomInfo[cnPatioID].nID),':',sRoomInfo[cnPatioID].strName,','
					  ,Itoa(sRoomInfo[cnGarageID].nID),':',sRoomInfo[cnGarageID].strName,','
					  ,Itoa(sRoomInfo[cnMBedID].nID),':',sRoomInfo[cnMBedID].strName,','
					  ,Itoa(sRoomInfo[cnMBathID].nID),':',sRoomInfo[cnMBathID].strName"
    
    // Common
    Send_Command vdvGlobalComms,"'GROUP2=',Itoa(sRoomInfo[cnFamilyID].nID),':',sRoomInfo[cnFamilyID].strName,','
					  ,Itoa(sRoomInfo[cnKitchenID].nID),':',sRoomInfo[cnKitchenID].strName,','
					  ,Itoa(sRoomInfo[cnMBathID].nID),':',sRoomInfo[cnMBathID].strName"
    // Exterior
    Send_Command vdvGlobalComms,"'GROUP3=',Itoa(sRoomInfo[cnPatioID].nID),':',sRoomInfo[cnPatioID].strName,','
					  ,Itoa(sRoomInfo[cnGarageID].nID),':',sRoomInfo[cnGarageID].strName"

    Send_Command vdvGlobalComms,"'SOURCE',Itoa(sSourceInfo[cnKitchenPCID].nSourceValue),'=',sSourceInfo[cnKitchenPCID].strName"
    Send_Command vdvGlobalComms,"'SOURCE',Itoa(sSourceInfo[cnFamilyIpodID].nSourceValue),'=',sSourceInfo[cnFamilyIpodID].strName"
    Send_Command vdvGlobalComms,"'SOURCE',Itoa(sSourceInfo[cnFamilyCdID].nSourceValue),'=',sSourceInfo[cnFamilyCdID].strName"
    Send_Command vdvGlobalComms,"'SOURCE',Itoa(sSourceInfo[cnSatMusicID].nSourceValue),'=',sSourceInfo[cnSatMusicID].strName"

    // Global Audio
    //Format: PANELid=DisplayLen
    Send_Command vdvGlobalComms,"'PANEL',Itoa(cnIpadID),'=8'"
    Send_Command vdvGlobalComms,"'PANEL',Itoa(cnFamilyTPID),'=4'"
    Send_Command vdvGlobalComms,"'PANEL',Itoa(cnMBedTPID),'=4'"
    Send_Command vdvGlobalComms,"'PANEL',Itoa(cnKitchenKPID),'=4'"
    Send_Command vdvGlobalComms,"'PANEL',Itoa(cnGarageKPID),'=4'"
    Send_Command vdvGlobalComms,"'PANEL',Itoa(cnMBathKPID),'=4'"

    Send_Command vdvGlobalShareComms,"'GROUP1=',Itoa(sRoomInfo[cnFamilyID].nID),':',sRoomInfo[cnFamilyID].strName,','
					  ,Itoa(sRoomInfo[cnKitchenID].nID),':',sRoomInfo[cnKitchenID].strName,','
					  ,Itoa(sRoomInfo[cnPatioID].nID),':',sRoomInfo[cnPatioID].strName,','
					  ,Itoa(sRoomInfo[cnGarageID].nID),':',sRoomInfo[cnGarageID].strName,','
					  ,Itoa(sRoomInfo[cnMBedID].nID),':',sRoomInfo[cnMBedID].strName,','
					  ,Itoa(sRoomInfo[cnMBathID].nID),':',sRoomInfo[cnMBathID].strName"


    Send_Command vdvGlobalShareComms,"'SOURCE',Itoa(sSourceInfo[cnFamilyDtvID].nSourceValue),'=',sSourceInfo[cnFamilyDtvID].strName"
    Send_Command vdvGlobalShareComms,"'SOURCE',Itoa(sSourceInfo[cnKitchenPCID].nSourceValue),'=',sSourceInfo[cnKitchenPCID].strName"
    Send_Command vdvGlobalShareComms,"'SOURCE',Itoa(sSourceInfo[cnFamilyIpodID].nSourceValue),'=',sSourceInfo[cnFamilyIpodID].strName"
    Send_Command vdvGlobalShareComms,"'SOURCE',Itoa(sSourceInfo[cnFamilyCdID].nSourceValue),'=',sSourceInfo[cnFamilyCdID].strName"
    Send_Command vdvGlobalShareComms,"'SOURCE',Itoa(sSourceInfo[cnSatMusicID].nSourceValue),'=',sSourceInfo[cnSatMusicID].strName"

    Send_Command vdvStat1,"'IPADDRESS=192.168.15.132'"
    Send_Command vdvStat1,"'USERNAME=admin'"
    Send_Command vdvStat1,"'PASSWORD=admin'"

    Send_Command vdvVHMAV,"'CLIENTKEY=',strClientKeyAV"

    Send_Command vdvVHMControlCallHome,"'CLIENTKEY=',strClientKeyControl"
    Send_Command vdvVHMControl,"'SERVERPORT=8494'"
    Send_Command vdvVHMControl,"'UPDATETIME=60'"
    Send_Command vdvVHMControl,"'CLIENTKEY=',strClientKeyControl"
    Send_Command vdvVHMControl,"'COLUMNS=1'"
    Send_Command vdvVHMControl,"'SCENENAME1=Header 1'"
    Send_Command vdvVHMControl,"'SCENENAME2=Scene2'"
    Send_Command vdvVHMControl,"'SCENENAME3=Scene3'"
    Send_Command vdvVHMControl,"'SCENENAME4=Header 2'"
    Send_Command vdvVHMControl,"'SCENENAME5=Scene5'"
    Send_Command vdvVHMControl,"'SCENENAME6=Scene6'"
    Send_Command vdvVHMControl,"'SCENENAME7=Scene7'"
    Send_Command vdvVHMControl,"'SCENENAME8=Header 3'"
    Send_Command vdvVHMControl,"'SCENENAME9=Scene9'"
    Send_Command vdvVHMControl,"'SCENENAME10=Scene10'"
    Send_Command vdvVHMControl,"'SCENEDESC2=Desc2'"
    Send_Command vdvVHMControl,"'SCENEDESC3=Desc3'"
    Send_Command vdvVHMControl,"'SCENEDESC5=Desc5'"
    Send_Command vdvVHMControl,"'SCENEDESC6=Desc6'"
    Send_Command vdvVHMControl,"'SCENEDESC7=Desc7'"
    Send_Command vdvVHMControl,"'SCENEDESC9=Desc9'"
    Send_Command vdvVHMControl,"'SCENEDESC10=Desc10'"
    Send_Command vdvVHMControl,"'SCENETYPE1=1'"
    Send_Command vdvVHMControl,"'SCENETYPE2=2'"
    Send_Command vdvVHMControl,"'SCENETYPE3=2'"
    Send_Command vdvVHMControl,"'SCENETYPE4=1'"
    Send_Command vdvVHMControl,"'SCENETYPE5=2'"
    Send_Command vdvVHMControl,"'SCENETYPE6=2'"
    Send_Command vdvVHMControl,"'SCENETYPE7=2'"
    Send_Command vdvVHMControl,"'SCENETYPE8=1'"
    Send_Command vdvVHMControl,"'SCENETYPE9=2'"
    Send_Command vdvVHMControl,"'SCENETYPE10=2'"
    Send_Command vdvVHMControl,"'STATUSNAME1=Audio / Video'"
    Send_Command vdvVHMControl,"'STATUSNAME2=Lighting'"
    Send_Command vdvVHMControl,"'STATUSNAME3=Climate'"
    Send_Command vdvVHMControl,"'STATUSNAME4=Security'"
    Send_Command vdvVHMControl,"'STATUSDESC1,1=All Off'"
    Send_Command vdvVHMControl,"'STATUSDESC1,2=Some On'"
    Send_Command vdvVHMControl,"'STATUSDESC1,3=Offline'"
    Send_Command vdvVHMControl,"'STATUSDESC2,1=All Lights Off'"
    Send_Command vdvVHMControl,"'STATUSDESC2,2=Some Lights On'"
    Send_Command vdvVHMControl,"'STATUSDESC2,3=Offline'"
    Send_Command vdvVHMControl,"'STATUSDESC3,1=Occupied'"
    Send_Command vdvVHMControl,"'STATUSDESC3,2=Unoccupied'"
    Send_Command vdvVHMControl,"'STATUSDESC3,3=Offline'"
    Send_Command vdvVHMControl,"'STATUSDESC4,1=Disarmed'"
    Send_Command vdvVHMControl,"'STATUSDESC4,2=Armed'"
    Send_Command vdvVHMControl,"'STATUSDESC4,3=Offline'"
    
    Wait 200
    {
	Send_Command vdvInterRoomComms,"'REBUILD_EVENTS'"
	Wait 100
	{
	    Send_Command vdvInterRoomComms,"'SWITCHROOM0=0'" // turn off every room for known starting point
	    Wait 20
		Send_Command vdvVHMControl,"'INITIALIZE'"
	}
    }
}

(***********************************************************)
(*            MODULE DEFINITIONS GO BELOW                  *)
(***********************************************************)
Define_Module 'AS8-AS16Mod' mdlAudioSwitcher(vdvAudioSwitcher,dvAudioSwitcher)

Define_Module 'i!-TimeManagerMod' mdlTM(vdvTmEvents,
                                        dvTmTimeSync,
                                        (* Timezone *)
                                        dTmTzName,
                                        dTmTzDesc,
                                        dTmTzGmtOffset,
                                        strTmTzDstRules,
                                        (* Location *)
                                        strTmLocName,
                                        dTmLocLong,
                                        dTmLocLat,
                                        (* Timeserver *)
                                        nTmTsProtocol,
                                        nTmTsCheckTime,
                                        strTmTsServer)

Define_Module 'Weather' mdlWeather(dvWeather, daAllWeatherUIs, nWeatherButtons, nWeatherTextChans, nWeatherFeedbackChans, strWeatherZipCodes, nWeatherZipCodes)

Define_Module 'DMS-IMSMod' mdlKitchenDMS(vdvKitchenKP,dvKitchenKP,sDMS[cnKitchenKPID].strFileName,sDMS[cnKitchenKPID].anVarText)
Define_Module 'DMS-IMSMod' mdlGarageDMS(vdvGarageKP,dvGarageKP,sDMS[cnGarageKPID].strFileName,sDMS[cnGarageKPID].anVarText)
Define_Module 'DMS-IMSMod' mdlMBathDMS(vdvMBathKP,dvMBathKP,sDMS[cnMBathKPID].strFileName,sDMS[cnMBathKPID].anVarText)

Define_Module 'AS16 - Audio Video Zone' mdlFamily(vdvFamilyAV, // Virtual Device For The Room
                                                    vdvInterRoomComms, // Inter Room Comm Virtual Device
						    daFamilyPanels, // Room Panels
						    vdvAudioSwitcher, // Audio Switcher
						    vdvDummySwitcher, // Video Switcher
						    vdvHDSwitcher, // HD Switcher
						    vdvFamilyAVR, // AVR
						    vdvFamilyTV, // TV
						    sRoomInfo[cnFamilyID].nAudioOutput, // Audio Output
						    sRoomInfo[cnFamilyID].nVideoOutput, // Video Output
						    sRoomInfo[cnFamilyID].nHDOutput, // HD Output
						    nRoomButtons, // Button Array
						    nRoomTextChans, // Text Channel Array
						    sRoomInfo[cnFamilyID].nID, // Room ID
						    sRoomInfo[cnFamilyID].strName) // Room Name

Define_Module 'AS16 - Audio Video Zone' mdlKitchen(vdvKitchenA, // Virtual Device For The Room
                                                    vdvInterRoomComms, // Inter Room Comm Virtual Device
						    daKitchenPanels, // Room Panels
						    vdvAudioSwitcher, // Audio Switcher
						    vdvDummySwitcher, // Video Switcher
						    vdvDummySwitcher, // HD Switcher
						    vdvDummyAVR, // AVR
						    vdvDummyTV, // TV
						    sRoomInfo[cnKitchenID].nAudioOutput, // Audio Output
						    sRoomInfo[cnKitchenID].nVideoOutput, // Video Output
						    sRoomInfo[cnKitchenID].nHDOutput, // HD Output
						    nRoomButtons, // Button Array
						    nRoomTextChans, // Text Channel Array
						    sRoomInfo[cnKitchenID].nID, // Room ID
						    sRoomInfo[cnKitchenID].strName) // Room Name

Define_Module 'AS16 - Audio Video Zone' mdlPatio(vdvPatioA, // Virtual Device For The Room
                                                    vdvInterRoomComms, // Inter Room Comm Virtual Device
						    daPatioPanels, // Room Panels
						    vdvAudioSwitcher, // Audio Switcher
						    vdvDummySwitcher, // Video Switcher
						    vdvDummySwitcher, // HD Switcher
						    vdvDummyAVR, // AVR
						    vdvDummyTV, // TV
						    sRoomInfo[cnPatioID].nAudioOutput, // Audio Output
						    sRoomInfo[cnPatioID].nVideoOutput, // Video Output
						    sRoomInfo[cnPatioID].nHDOutput, // HD Output
						    nRoomButtons, // Button Array
						    nRoomTextChans, // Text Channel Array
						    sRoomInfo[cnPatioID].nID, // Room ID
						    sRoomInfo[cnPatioID].strName) // Room Name

Define_Module 'AS16 - Audio Video Zone' mdlGarage(vdvGarageA, // Virtual Device For The Room
                                                    vdvInterRoomComms, // Inter Room Comm Virtual Device
						    daGaragePanels, // Room Panels
						    vdvAudioSwitcher, // Audio Switcher
						    vdvDummySwitcher, // Video Switcher
						    vdvDummySwitcher, // HD Switcher
						    vdvDummyAVR, // AVR
						    vdvDummyTV, // TV
						    sRoomInfo[cnGarageID].nAudioOutput, // Audio Output
						    sRoomInfo[cnGarageID].nVideoOutput, // Video Output
						    sRoomInfo[cnGarageID].nHDOutput, // HD Output
						    nRoomButtons, // Button Array
						    nRoomTextChans, // Text Channel Array
						    sRoomInfo[cnGarageID].nID, // Room ID
						    sRoomInfo[cnGarageID].strName) // Room Name

Define_Module 'AS16 - Audio Video Zone' mdlMBath(vdvMBathA, // Virtual Device For The Room
                                                    vdvInterRoomComms, // Inter Room Comm Virtual Device
						    daMBathPanels, // Room Panels
						    vdvAudioSwitcher, // Audio Switcher
						    vdvDummySwitcher, // Video Switcher
						    vdvDummySwitcher, // HD Switcher
						    vdvDummyAVR, // AVR
						    vdvDummyTV, // TV
						    sRoomInfo[cnMBathID].nAudioOutput, // Audio Output
						    sRoomInfo[cnMBathID].nVideoOutput, // Video Output
						    sRoomInfo[cnMBathID].nHDOutput, // HD Output
						    nRoomButtons, // Button Array
						    nRoomTextChans, // Text Channel Array
						    sRoomInfo[cnMBathID].nID, // Room ID
						    sRoomInfo[cnMBathID].strName) // Room Name
/*
Define_Module 'Paragon - Global Audio' mdlGlobalAudio(vdvInterRoomComms,
						      vdvGlobalComms,
						      daAllRooms,
						      daAllUIs,
						      nGlobalGroupSelectButtons,
						      nGlobalZoneSelectButtons,
						      nGlobalSourceSelectButtons,
						      nClearZoneSelectButton,
						      nGlobalScrollUpButton,
						      nGlobalScrollDownButton,
						      nGlobalVolumeUpButton,
						      nGlobalVolumeDownButton,
						      nGlobalDisableConnectChan,
						      nGlobalRoomTextChans,
						      nGlobalSourceTextChans,
						      nGlobalVolumeTextChans)
*/
Define_Module 'Paragon - Global Audio Share Source' mdlGlobalAudioShareSource(vdvInterRoomComms,
						      vdvGlobalShareComms,
						      daAllRooms,
						      daAllUIs,
						      nGlobalGroupSelectButtons,
						      nGlobalZoneSelectButtons,
						      nGlobalSelectedZoneOffButton,
						      nGlobalVolumeUpButton,
						      nGlobalVolumeDownButton,
						      nGlobalDisableConnectChan,
						      nGlobalListPort,
						      nGlobalListAddress,
						      nGlobalListViewAddress)


Define_Module 'IR TV' mdlFamilyTV (dvFamilyTV, vdvFamilyTV)
Define_Module 'Sony_STR-DA7ES' mdlFamilyRcvr(dvFamilyAVR,vdvFamilyAVR)

Define_Module 'IR DVD' mdlFamilyDVD (dvFamilyDVD, vdvFamilyDVD, daFamilyDvdPanels)

Define_Module 'IR CD' mdlFamilyCD (dvFamilyCD, vdvFamilyCD, daFamilyCDPanels)

Define_Module 'DTV HR21 IP Driver' mdlFamilyDTV(dvFamilyDTV, vdvFamilyDTV, strFamilyDTVIpAddress)
Define_Module 'DTV Dynamic Preset Client' mdlFamilyDTV_GUI(vdvFamilyDTV,
						    daFamilyDtvPanels,
						    nSatButtons,
						    nSatPresetButtons,
						    nSatPresetBmpChans,
						    nSatPresetTextChans,
						    nSatCurrentInfoChans,
						    strSatFavsFileName)

Define_Module 'DTV Music Client' mdlSatMusicDTV_GUI(dvDTV_IP,
						    vdvFamilyDTV,
						    daFamilySatMusicPanels,
						    nSatMusicPresetButtons,
						    nSatMusicPresetTextChans,
						    strSatMusicFavsFileName,
						    nSlaveSat)
/*
Define_Module 'DTV Music Client' mdlSatMusicDTV_TPs(dvDTV_IP,
						    vdvFamilyDTV,
						    daSatMusicTPs,
						    nTPSatMusicPresetButtons,
						    nTPSatMusicPresetTextChans,
						    strSatMusicFavsFileName,
						    nSlaveSat)

Define_Module 'DTV Music Client DMS' mdlSatMusicDTV_DMS(dvDTV_IP,
						    vdvFamilyDTV,
						    daSatMusicDMS,
						    nSatMusicPresetButtons,
						    nSatMusicPresetTextChans,
						    strSatMusicFavsFileName,
						    nSlaveSat)
*/
//Define_Module 'Proliphix' mdlStat(dvStat1, vdvStat1, daAllWeb, vdvFamilyWeb, nPanelType, nStatChans, nVarTextAddr)

Define_Module 'VIA - VHM Status and Call Home' mdlVHM(dvVHMAV, vdvVHMAV)
Define_Module 'VIA - VHM Status and Call Home' mdlVHM(dvVHMControlCallHome, vdvVHMControlCallHome)
Define_Module 'VIA - VHM Control' mdlVHM(dvVHMControl, vdvVHMControl)

Define_Module 'Timer Control' mdlTimer1(dvRelay, daAllTimerTps, nTimer1RelayChans, nTimer1Buttons, nTimer1VarText, strTimer1FileName)
Define_Module 'Timer Control' mdlTimer2(dvRelay, daAllTimerTps, nTimer2RelayChans, nTimer2Buttons, nTimer2VarText, strTimer2FileName)
Define_Module 'Timer Control' mdlTimer3(dvRelay, daAllTimerTps, nTimer3RelayChans, nTimer3Buttons, nTimer3VarText, strTimer3FileName)
Define_Module 'Timer Control' mdlTimer4(dvRelay, daAllTimerTps, nTimer4RelayChans, nTimer4Buttons, nTimer4VarText, strTimer4FileName)
(***********************************************************)
(*                THE EVENTS GO BELOW                      *)
(***********************************************************)
DEFINE_EVENT

DATA_EVENT[dvAudioSwitcher]  // actual device
{
    ONLINE:
    {
	LOCAL_VAR
	INTEGER nI
		
	Send_Command vdvAudioSwitcher,'FBON'  // SEND UNSOLICTED FEEDBACK
	Wait 5
	{
	    Send_Command vdvAudioSwitcher,'POLL0'  // GET CURRENT STATUS right now
	}
    }
}

DATA_EVENT[vdvAudioSwitcher]  // Virtual device
{
  STRING:
  {
/*
    sAudioSwitcher[1].strBUFF="sAudioSwitcher[1].strBUFF,DATA.TEXT"
    sAudioSwitcher[1].strDATA=Remove_String(sAudioSwitcher[1].strBUFF,"13",1)  // CARRIAGE RETURN AT THE END OF ALL RESPONSES
		Call 'DEBUG_OUTPUT' (__LINE__,"'sAudioSwitcher[1].strDATA:',sAudioSwitcher[1].strDATA")
    If (sAudioSwitcher[1].strDATA)  
    {
      If (Find_String(sAudioSwitcher[1].strDATA,'AI',1))  // FIND WHAT INPUT
      {
        strJunk = Remove_String(sAudioSwitcher[1].strDATA,'AI',1)
        sAudioSwitcher[1].nInput=ATOI(sAudioSwitcher[1].strDATA)
        While (Find_String(sAudioSwitcher[1].strDATA,'O',1))  // FIND WHAT OUTPUT
        {
          //Cancel_Wait 'AS16_1_Wait'
					strJunk = Remove_String(sAudioSwitcher[1].strDATA,'O',1)
          sAudioSwitcher[1].nOutput=ATOI(sAudioSwitcher[1].strDATA)
          If (sAudioSwitcher[1].nOutput>=1 && sAudioSwitcher[1].nOutput<=16)
          {
            //nLastSource[1][sAudioSwitcher[1].nOutput] = sAudioSwitcher[1].nOutputStatus[sAudioSwitcher[1].nOutput]  // store off the last source
            sAudioSwitcher[1].nOutputStatus[sAudioSwitcher[1].nOutput]=sAudioSwitcher[1].nInput
            Off[vdvAudioSwitcher,(sAudioSwitcher[1].nOUTPUT*3)]  // UNMUTE AUDIO
						Call 'DEBUG_OUTPUT' (__LINE__,"'AudioSwitcher Output:',Itoa(sAudioSwitcher[1].nOutput),' to input ',Itoa(sAudioSwitcher[1].nOutputStatus[sAudioSwitcher[1].nOutput])")
            If (sAudioSwitcher[1].nLevel[sAudioSwitcher[1].nOutput] <= 20)
            {
              Send_Command vdvAudioSwitcher,"'SL',ITOA(sAudioSwitcher[1].nOutput),'V',ITOA(sAudioSwitcher[1].nLowVol[sAudioSwitcher[1].nOutput]),'%'"
            }
            Else If (sAudioSwitcher[1].nLEVEL[sAudioSwitcher[1].nOUTPUT] > 60)
            {
              Send_Command vdvAudioSwitcher,"'SL',ITOA(sAudioSwitcher[1].nOutput),'V',ITOA(sAudioSwitcher[1].nHighVol[sAudioSwitcher[1].nOutput]),'%'"
            }
						//Wait 2 'AS16_1_Wait'
            //Call 'UPDATE_WEBCONTROL' (sAudioSwitcher[1].nOutput)
          }
        }
        sAudioSwitcher[1].strDATA=''
      }
    }
*/
  }
}

LEVEL_EVENT [vdvAudioSwitcher,nAudioSwitcherLevels]
{
  //If (!nKillLevel)
  //{
    sAudioSwitcher[1].nLevel[Level.Input.Level]=(Level.Value/2.55)
		Call 'DEBUG_OUTPUT' (__LINE__,"'Zone:',Itoa(Level.Input.Level),' Vol:',Itoa(sAudioSwitcher[1].nLevel[Level.Input.Level])")
  /*  Switch (Level.Input.Level)
    {
		  Case cnAS16_1_OutKitchen:
		  {
				Send_Command vdvFamilyWeb,"'TEXT',Itoa(vdvKitchenWeb.Number),',1-',Itoa(sAudioSwitcher[1].nLevel[Level.Input.Level])"
			} 
		  Case cnAS16_1_OutMBath:
		  {
				Send_Command vdvFamilyWeb,"'TEXT',Itoa(vdvMBathWeb.Number),',1-',Itoa(sAudioSwitcher[1].nLevel[Level.Input.Level])"
			} 
		  Case cnAS16_1_OutPatio:
		  {
				Send_Command vdvFamilyWeb,"'TEXT',Itoa(vdvPatioWeb.Number),',1-',Itoa(sAudioSwitcher[1].nLevel[Level.Input.Level])"
			} 
		  Case cnAS16_1_OutGarage:
		  {
				Send_Command vdvFamilyWeb,"'TEXT',Itoa(vdvGarageWeb.Number),',1-',Itoa(sAudioSwitcher[1].nLevel[Level.Input.Level])"
			} 
		} */
  //}
}

Level_Event[daAllRooms,cnRoomSourceValueLevelChan]
{
    sRoomInfo[Get_last(daAllRooms)].nCurrentSource = Level.Value
}

Data_Event[vdvInterRoomComms]
{
    Command:
    {
	Local_Var Integer nSource, nAnyRoomOn, nI
	
	If (Find_String(Data.Text,'INUSE',1)) // Used for power management and in use indicator feedback
	{
	    strJunk = Remove_String(Data.Text,'INUSE',1)
	    nSource = Atoi(Data.Text)
	    If (nSource > 0 && nSource <= cnMaxSources)
	    {
		nAnyRoomOn = 0
		For (nI = 1; nI<= cnMaxRooms; nI++) // Loop through all rooms
		{
		    nAnyRoomOn = nAnyRoomOn + sRoomInfo[nI].nCurrentSource // Add every rooms source values together
		}
		If (nAnyRoomOn == 0) // If every room is off the result will be 0
		{
		    For (nI = 1; nI <= cnMaxSources; nI++) // since every room is off clear all in use values
		    {
			sSourceInfo[nI].nInUse = 0
		    }
		}
		
		If ( Find_String(Data.Text,'++',1))
		{
		    sSourceInfo[nSource].nInUse++
		    Send_Command vdvInterRoomComms,"'INUSE',Itoa(nSource),'=',Itoa(sSourceInfo[nSource].nInUse)"
		    If (sSourceInfo[nSource].nInUse == 1) // Turn Source On
		    {
			Switch (nSource)
			{
			    Case cnFamilyDtvID:
			    {
				On[daFamilyPanels,61]
				On[daAllUIs,67]
				Pulse[vdvFamilyDTV,27]
			    }
			    Case cnFamilyDvdID:
			    {
				On[daFamilyPanels,62]
				Wait 5
				    Call 'PULSE_IR' (dvFamilyDVD, 1)
			    }
			    Case cnFamilyPs3ID:
			    {
				On[daFamilyPanels,63]
			    }
			    Case cnKitchenPCID:
			    {
				On[daAllUIs,64]
			    }
			    Case cnFamilyIpodID:
			    {
				On[daAllUIs,65]
			    }
			    Case cnFamilyCdID:
			    {
				On[daAllUIs,66]
				Wait 5
				    Call 'PULSE_IR' (dvFamilyCD, 1)
			    }
			    Case cnSatMusicID:
			    {
				On[daFamilyPanels,61]
				On[daAllUIs,67]
				Pulse[vdvFamilyDTV,27]
			    }
			}
		    }
		}
		Else If (Find_String(Data.Text,'--',1))
		{
		    sSourceInfo[nSource].nInUse = 0
		    For (nI = 1; nI <= cnMaxRooms; nI++)
		    {
			If (sRoomInfo[nI].nCurrentSource == nSource)
			{
			    sSourceInfo[nSource].nInUse++
			}
		    }
		    //If (sSourceInfo[nSource].nInUse > 0)
			//sSourceInfo[nSource].nInUse--
		    Send_Command vdvInterRoomComms,"'INUSE',Itoa(nSource),'=',Itoa(sSourceInfo[nSource].nInUse)"
		    
		    If (sSourceInfo[nSource].nInUse == 0) // Turn Source Off
		    {
			Switch (nSource)
			{
			    Case cnFamilyDtvID:
			    {
				If (sSourceInfo[cnSatMusicID].nInUse == 0)
				{
				    Off[daFamilyPanels,61]
				    Off[daAllUIs,67]
				    Pulse[vdvFamilyDTV,28]
				}
			    }
			    Case cnFamilyDvdID:
			    {
				Off[daFamilyPanels,62]
				Wait 5
				    Call 'PULSE_IR' (dvFamilyDVD, 28)
			    }
			    Case cnFamilyPs3ID:
			    {
				Off[daFamilyPanels,63]
			    }
			    Case cnKitchenPCID:
			    {
				Off[daAllUIs,64]
			    }
			    Case cnFamilyIpodID:
			    {
				Off[daAllUIs,65]
			    }
			    Case cnFamilyCdID:
			    {
				Off[daAllUIs,66]
				Wait 5
				    Call 'PULSE_IR' (dvFamilyCD, 28)
			    }
			    Case cnSatMusicID:
			    {
				If (sSourceInfo[cnFamilyDtvID].nInUse == 0)
				{
				    Off[daFamilyPanels,61]
				    Off[daAllUIs,67]
				    Pulse[vdvFamilyDTV,28]
				}
			    }
			}
		    }
		}
	    }
	}
    }
}

// VHM TEst Code
Channel_Event[daAllRooms,1]
{
    On:
    {
	Local_Var Integer nRoomsOn
	nRoomsOn = ![vdvFamilyAV,1] + ![vdvKitchenA,1] + ![vdvPatioA,1] + ![vdvGarageA,1] + ![vdvMBedAV,1] + ![vdvMBathA,1]
	Call 'DEBUG_OUTPUT' (__LINE__,"'nRoomsOn=',itoa(nRoomsOn)")
	If (nRoomsOn == 6) // all on
	    Send_Level vdvVHMAV,1,1
	Else If (nRoomsOn > 0) // some on
	    Send_Level vdvVHMAV,1,3
	Else // all off
	    Send_Level vdvVHMAV,1,2
	[vdvVHMControl,2] = sRoomInfo[cnFamilyID].nCurrentSource = cnFamilyIpodID // If current source is family ipod set the led otherwise led off.
	[vdvVHMControl,3] = [vdvFamilyAV,1] // led on if room is off
    }
    Off:
    {
	Local_Var Integer nRoomsOn
	nRoomsOn = ![vdvFamilyAV,1] + ![vdvKitchenA,1] + ![vdvPatioA,1] + ![vdvGarageA,1] + ![vdvMBedAV,1] + ![vdvMBathA,1]
	Call 'DEBUG_OUTPUT' (__LINE__,"'nRoomsOn=',itoa(nRoomsOn)")
	If (nRoomsOn == 6) // all on
	    Send_Level vdvVHMAV,1,1
	Else If (nRoomsOn > 0) // some on
	    Send_Level vdvVHMAV,1,3
	Else // all off
	    Send_Level vdvVHMAV,1,2
	[vdvVHMControl,2] = sRoomInfo[cnFamilyID].nCurrentSource = cnFamilyIpodID // If current source is family ipod set the led otherwise led off.
	[vdvVHMControl,3] = [vdvFamilyAV,1] // led on if room is off
    }
}

Button_Event[vdvVHMControl,2]
{
    Push:
    {
	Send_Command vdvInterRoomComms,"'SWITCHROOM',Itoa(cnFamilyID),'=',Itoa(cnFamilyIpodID)"
    }
}
Button_Event[vdvVHMControl,3]
{
    Push:
    {
	Send_Command vdvInterRoomComms,"'SWITCHROOM',Itoa(cnFamilyID),'=0'"
    }
}

Button_Event[vdvVHMControl,5]
Button_Event[vdvVHMControl,6]
Button_Event[vdvVHMControl,7]
Button_Event[vdvVHMControl,9]
Button_Event[vdvVHMControl,10]
{
    Push:
    {
	[vdvVHMControl,Button.Input.Channel] = ![vdvVHMControl,Button.Input.Channel]
    }
}

(***********************************************************)
(*            THE ACTUAL PROGRAM GOES BELOW                *)
(***********************************************************)
DEFINE_PROGRAM

(***********************************************************)
(*                     END OF PROGRAM                      *)
(*        DO NOT PUT ANY CODE BELOW THIS COMMENT           *)
(***********************************************************)